package com.gem.back.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gem.back.entity.*;
import com.gem.back.service.*;
import com.gem.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 * 公告管理
 * @author hhhy
 * @since 2020-02-22
 */
@Controller
@RequestMapping("/notice")
public class AoaNoticeListController {
    @Autowired
    private IAoaNoticeListService aoaNoticeListService;
    @Autowired
    private IAoaTypeListService aoaTypeListService;
    @Autowired
    private IAoaStatusListService aoaStatusListService;
    @Autowired
    private IAoaUserService aoaUserService;
    @Autowired
    private IAoaNoticeUserRelationService aoaNoticeUserRelationService;
    //加载通知管理列表
    @RequestMapping("/manage")
    public String noticeManage(String title,String page, Model model){
        //默认配置
        Integer currentPage = 1;
        Integer pageCount = 10;
        if (title == null){
            title = "";
        }
        if (page != null){
            currentPage = Integer.valueOf(page);
        }

        List<AoaNoticeList> noticeLists = aoaNoticeListService.findNoticeList(currentPage,pageCount,title);
        int totalPage =aoaNoticeListService.getTotalPage(pageCount,title);
        model.addAttribute("noticeLists",noticeLists);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("currentPage",currentPage);
        model.addAttribute("title",title);
        return "notice/informmanage";
    }

    //新增公告
    @RequestMapping("/edit")
    public String noticeEdit(Integer noticeId,AoaNoticeList aoaNoticeList,Model model){
        QueryWrapper<AoaTypeList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type_model","inform");
        List<AoaTypeList> typeList =  aoaTypeListService.list(queryWrapper);
        QueryWrapper<AoaStatusList> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("status_model","inform");
        List<AoaStatusList> statusList = aoaStatusListService.list(queryWrapper1);
        if (noticeId != null){
            aoaNoticeList = aoaNoticeListService.findNoticeById(noticeId);
        }
        model.addAttribute("aoaNoticeList",aoaNoticeList);
        model.addAttribute("typeList",typeList);
        model.addAttribute("statusList",statusList);
        return "notice/informedit";
    }

    @RequestMapping("/save")
    public String save(AoaNoticeList noticeList,HttpServletRequest request){
        aoaNoticeListService.saveNotice(noticeList,request);
        return "redirect:manage";
    }

    //显示公告详细信息
    @RequestMapping("/show")
    public String showInform(Integer noticeId,AoaNoticeList aoaNoticeList2,Model model){
        if (noticeId != null){
            aoaNoticeList2 = aoaNoticeListService.findNoticeById(noticeId);
        }
        model.addAttribute("aoaNoticeList2",aoaNoticeList2);
        return "notice/informshow";
    }

    //删除通知管理
    @ResponseBody
    @RequestMapping("/delete")
    public ResponseResult deleteNotice(Integer noticeId){
        boolean flag = aoaNoticeListService.removeById(noticeId);
        ResponseResult result = null;
        if (flag == true){
            result = new ResponseResult("200","删除成功！");
        }else {
            result = new ResponseResult("200","删除失败！");
        }
        return result;
    }
//    @RequestMapping("/delete")
//    public String deleteNotice(Integer noticeId){
//        aoaNoticeListService.removeById(noticeId);
//        return "redirect:manage";
//    }

    //加载通知列表
    @RequestMapping("/list")
    public String listPage(String title,String page, Model model,Integer id){
        //默认配置
        Integer currentPage = 1;
        Integer pageCount = 10;
        if (title == null){
            title = "";
        }
        if (page != null){
            currentPage = Integer.valueOf(page);
        }
        UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext()
                .getAuthentication()
                .getPrincipal();
        AoaUser aoaUser = aoaUserService.findUserByName(userDetails.getUsername());
        id = aoaUser.getUserId();
        List<AoaNoticeList> noticeLists = aoaNoticeListService.findMyNoticeList(currentPage,pageCount,title,id);
        int totalPage = aoaNoticeListService.getMyTotalPage(pageCount,title,id);
        model.addAttribute("noticeLists",noticeLists);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("currentPage",currentPage);
        model.addAttribute("title",title);
        return "notice/informlist";
    }

//    @RequestMapping("/deleteMyNotice")
//    public String deleteMyNotice(Integer noticeId){
//        AoaNoticeList aoaNoticeList = aoaNoticeListService.findNoticeById(noticeId);
//        aoaNoticeList.setDel_flag(1);
//        aoaNoticeListService.updateById(aoaNoticeList);
//        return "redirect:list";
//    }

    //删除通知列表(逻辑删除)
    @ResponseBody
    @RequestMapping("/deleteMyNotice")
    public ResponseResult deleteMyNotice(@SessionAttribute("userId")Integer userId,Integer noticeId){
        AoaNoticeUserRelation aoaNoticeUserRelation = aoaNoticeUserRelationService.findNoticeUserRelationById(noticeId,userId);
        aoaNoticeUserRelation.setDel_flag(1);
        aoaNoticeUserRelationService.updateById(aoaNoticeUserRelation);
        System.out.println("===============================================" + aoaNoticeUserRelation.getDel_flag());
        ResponseResult result = null;
        if (aoaNoticeUserRelation.getDel_flag() == 1){
            result = new ResponseResult("200","删除成功！");
        }else {
            result = new ResponseResult("200","删除失败！");
        }
        return result;
    }

    //转发给下属
    @ResponseBody
    @RequestMapping("/forwardAnother")
    public ResponseResult forwardAnother(@SessionAttribute("userId")Integer fatherId,Integer noticeId){
        List<AoaUser> aoaUsers = aoaUserService.findUserByFatherId(fatherId);
        System.out.println(aoaUsers);
        AoaNoticeUserRelation aoaNoticeUserRelation = aoaNoticeUserRelationService.findNoticeUserRelationById(noticeId,fatherId);
        aoaNoticeUserRelation.setForward(1);
        aoaNoticeUserRelationService.updateById(aoaNoticeUserRelation);
        List<AoaNoticeUserRelation> noticeUserRelations = new ArrayList<>();
        for (AoaUser user : aoaUsers){
            noticeUserRelations.add(new AoaNoticeUserRelation(0,noticeId,user.getUserId()));
        }
        aoaNoticeUserRelationService.saveBatch(noticeUserRelations);
        ResponseResult result = null;
        if (aoaNoticeUserRelation.getForward() == 1){
            result = new ResponseResult("200","转发成功！");
        }else {
            result = new ResponseResult("200","转发失败！");
        }
        return result;
    }

}
